package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DxshAppBanner;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.customBo.DxshAppUserChannelBo;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppBannerService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppQuickAccessService;
import com.syni.mdd.sjb.common.datatreaure.service.DxshAppUserChannelService;
import com.syni.mdd.sjb.common.datatreaure.constants.Constants;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.index.DataRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.userChannel.GetMyChannelRpVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.index.DataVo;
import com.syni.mdd.sjb.common.datatreaure.listen.appNews.AppNewsSwitchEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @className DxshAppIndexService
 * @description TOO
 * @Author cfx
 * @DATE 2020/4/21 17:29
 * @VERSION 1.0
 **/
@Service
public class DxshAppIndexServiceImpl {
    @Autowired
    DxshAppQuickAccessService quickAccessService;

    @Autowired
    DxshAppUserChannelService dxshAppUserChannelService;

    @Autowired
    DxshAppBannerService dxshAppBannerService;

    public Map<String, Object> data(DataVo dataVo) {
        DataRpVo dataRpVo = new DataRpVo();
        //获取用户id
        Integer userId = dataVo.getUserId();
        //获取快捷访问记录表
        List<DataRpVo.DxshAppQuickAccessVo> dxshAppQuickAccessVoList = dxshAppUserChannelService.getIndexQuickAccessData(userId);
        if(dxshAppQuickAccessVoList != null && dxshAppQuickAccessVoList.size() > 0){
            dataRpVo.setDxshAppQuickAccess(dxshAppQuickAccessVoList);
        }
        //获取未读消息
        //获取首页banner
        List<DxshAppBanner> dxshAppBanners = dxshAppBannerService.getIndexBanner();
        List<DataRpVo.DxshAppBannerVo> dxshAppBannerVos = BeanUtils.copyList(dxshAppBanners, DataRpVo.DxshAppBannerVo.class);
        dataRpVo.getDxshAppBanner().addAll(dxshAppBannerVos);
        //获取渠道信息
        List<DxshAppUserChannelBo> bos = dxshAppUserChannelService.getUserChannelGroupByType(userId);
        List<GetMyChannelRpVo> getMyChannelRpVo = BeanUtils.copyList(bos,GetMyChannelRpVo.class);
        dataRpVo.setMyChannels(getMyChannelRpVo);
        if(bos.size() > 0){
            dataRpVo.setHaveChannel(Constants.COMMON_FLAG_TRUE_INT);
        }
        //默认开启消息推送
        ApplicationContextHolder.getApplicationContext().
                publishEvent(new AppNewsSwitchEvent("initNewSwitch",userId));
        return ResultMapHelper.success(dataRpVo, ResultMsg.MSG_GET_SUCCESS);
    }
}
